package com.h3bpm.web.controller;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.RestfulApi.cloudpivot.ResponseResult;
import OThinker.H3.Entity.DataModel.BizObject;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.web.service.OutsourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 工序委外流程
 */
@RestController
@RequestMapping("/api/outsource")
public class OutsourceController extends ControllerBase {
    private static final Logger logger = LoggerFactory.getLogger(OutsourceController.class);
    @Autowired
    private OutsourceService outsourceService;

    @RequestMapping("/updateExecutableTrue")
    public ResponseResult updateExecutableTrue(String objectId){
        logger.info("开始更改工序委外流程为可出门状态,流程objectId：{}",objectId);
        ResponseResult rst = new ResponseResult(-1L,"",null);
        boolean bool = outsourceService.updateExecutableTrue(objectId);
        if (bool){
            rst.setErrmsg("成功");
            rst.setErrcode(0L);
            logger.info("更改工序委外流程为可出门状态成功,流程objectId：{}",objectId);
        }else {
            rst.setErrmsg("失败");
            logger.info("更改工序委外流程为可出门状态失败,流程objectId：{}",objectId);
        }
        return rst;
    }

    @RequestMapping("/updateExecutableFalse")
    public ResponseResult updateExecutableFalse(String objectId){
        logger.info("开始更改工序委外流程为不可出门状态,流程objectId：{}",objectId);
        ResponseResult rst = new ResponseResult(-1L,"",null);
        boolean bool = outsourceService.updateExecutableFalse(objectId);
        if (bool){
            rst.setErrmsg("成功");
            rst.setErrcode(0L);
            logger.info("更改工序委外流程为不可出门状态成功,流程objectId：{}",objectId);
        }else {
            rst.setErrmsg("失败");
            logger.info("更改工序委外流程为不可出门状态失败,流程objectId：{}",objectId);
        }
        return rst;
    }

    /**
     *  出门数据验证
     * @param objectId
     * @return
     */
    @RequestMapping("/processOutValid")
    public Object processOutValid(String objectId){

        JSONObject obj = new JSONObject();

        try {
            logger.debug("objectId:{}",objectId);

            long l1 = System.currentTimeMillis();

            Object o = outsourceService.processOutValid(objectId);

            long l3 = System.currentTimeMillis();
            if (logger.isDebugEnabled()){
                logger.info("service 服务时长:{}",l3-l1);
            }

            obj.put("validProcessOutFlag",o);


        } catch (Exception e) {
            e.printStackTrace();
            obj.put("validProcessOutFlag","系统异常");
        }


        return obj;
    }
    @RequestMapping("/setProcessInDate")
    public Object setProcessInDate(String objectId){
        long l1 = System.currentTimeMillis();
        logger.info("开始更新工序委外流程主数据信息,流程objectId：{}",objectId);
        JSONObject obj = new JSONObject();
        Object object = outsourceService.updateProcessIn(objectId);
        obj.put("validFlag",object);
        long l3 = System.currentTimeMillis();
        if (logger.isDebugEnabled()){
            logger.info("入库service 服务时长:{}",l3-l1);
        }
        return obj;
    }

    /**
     *  结算数据校验并回写
     * @param objectId
     * @return
     */
    @RequestMapping("settleDatavalid")
    public Object settleDatavalid(String objectId){

        logger.info("结算回写开始");
        JSONObject obj = new JSONObject();
        logger.debug("objectId:{}",objectId);

        long l1 = System.currentTimeMillis();
        Object o = outsourceService.settleDataWriteBack(objectId);

        long l3 = System.currentTimeMillis();
        if (logger.isDebugEnabled()){
            logger.info("service 服务时长:{}",l3-l1);
        }

        obj.put("validProcessOutFlag",o);

        return obj;
    }
    @RequestMapping("processInDetails")
    public Object processInDetails(String objectId,String outsourceTitle,String partnerUnit,String applicationUnitId){
        boolean bool = outsourceService.processInDetails(objectId, outsourceTitle, partnerUnit, applicationUnitId);
        return bool;
    }


    @RequestMapping("monthlySettlementDetails")
    public Object monthlySettlementDetails(String objectId,String partnerUnit,String applicationUnitId){
        boolean bool = outsourceService.monthlySettlementDetails(objectId, partnerUnit, applicationUnitId);
        return bool;
    }

    @RequestMapping("processInCleanUp")
    public Object processInCleanUp(String objectId){
        boolean bool = outsourceService.processInCleanUp(objectId);
        return bool;
    }

    @RequestMapping("monthlySettlementCleanUp")
    public Object monthlySettlementCleanUp(String objectId){
        boolean bool = outsourceService.monthlySettlementCleanUp(objectId);
        return bool;
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
